package com.ciko.code.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 项目名称：association
 * 包名称：com.ciko.code.utils
 * 类描述：
 * 创建人： wayne
 * 创建时间 2016/2/1 10:38.
 */
public class DateUtil {
    /* Date类型转化到String类型 */
    public static String DateToString(Date date, int type) {
        String result = null;
        String format = "yyyy-MM-dd";
        if (type == 0)
        {
            format = "yyyy-MM-dd";
        }
        if (type == 1)
        {
            format = "yyyy/MM/dd";
        }
        if (type == 11)
        {
            format = "yyyy-MM";
        }
        else if (type == 2)
        {
            format = "yyyyMMdd";
        }
        else if (type == 21)
        {
            format = "yyyyMM";
        }
        else if (type == 3)
        {
            format = "MM/dd/yy";
        }
        else if (type == 4)
        {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        else if (type == 5)
        {
            format = "MM月dd日 HH:mm:ss";
        }
        else if (type == 6)
        {
            format = "yyyyMMddHHmmss";
        }
        else if (type == 7)
        {
            format = "yyyyMMddHHmmssSSS";
        }

        SimpleDateFormat formatter = new SimpleDateFormat(format);
        if (date != null)
        {
            result = formatter.format(date);
        }
        else
        {
            result = "";
        }
        return result;
    }

    /* String类型到Date类型转化 */
    public static java.util.Date StringToDate(String strDate, int type) {
        java.util.Date result = null;
        String format = "yyyy-MM-dd";
        if (type == 0)
        {
            format = "yyyy-MM-dd";
        }
        if (type == 1)
        {
            format = "yyyy/MM/dd";
        }
        else if (type == 2)
        {
            format = "yyyyMMdd";
        }
        else if (type == 21)
        {
            format = "yyyyMM";
        }
        else if (type == 3)
        {
            format = "MM/dd/yy";
        }
        else if (type == 4)
        {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        else if (type == 5)
        {
            format = "yyyyMMddHHmmss";
        }
        else if (type == 6)
        {
            format = "yyyyMMddHHmm";
        }else if (type == 7)
        {
            format = "yyyy年MM月dd日";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        if (strDate != null && !strDate.equals(""))
        {
            try
            {
                result = formatter.parse(strDate);
            }
            catch (ParseException ex)
            {
                result = null;
            }
        }
        return result;
    }

    /* 时间long转化到Date类型 */
    public static String longToDate(long date, int type) {
        String format = "yyyy-MM-dd";
        if (type == 0)
        {
            format = "yyyy-MM-dd";
        }
        if (type == 1)
        {
            format = "yyyy/MM/dd";
        }
        if (type == 11)
        {
            format = "yyyy-MM";
        }
        else if (type == 2)
        {
            format = "yyyyMMdd";
        }
        else if (type == 21)
        {
            format = "yyyyMM";
        }
        else if (type == 3)
        {
            format = "MM/dd/yy";
        }
        else if (type == 4)
        {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        else if (type == 5)
        {
            format = "MM月dd日 HH:mm:ss";
        }
        else if (type == 6)
        {
            format = "yyyyMMddHHmmss";
        }
        else if (type == 7)
        {
            format = "yyyyMMddHHmmssSSS";
        }
        else if (type == 8)
        {
            format = "yyyy-MM-dd HH:mm";
        }

        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(new Date(date));
    }

    private static Calendar c    = Calendar.getInstance();

    public static int getYear() {
        return c.get(Calendar.YEAR);
    }

    public static int getMonth() {
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getDay() {
        return c.get(Calendar.DAY_OF_MONTH);
    }

    public static void updateCalender(Calendar can) {
        c = can;
    }

    /**
     * 根据月份获得最大天数
     *
     * @param year
     *            年
     * @param month
     *            月
     * @return 最大天数
     */
    public static int getMaxDayByMonth(int year, int month) {
        Calendar time = Calendar.getInstance();//使用默认时区和语言环境获得一个日历
        //注意：在使用set方法之前，必须先调用clear（），否则很多信息会继承自系统当前的时间
        time.clear();
        time.set(Calendar.YEAR, year);
        time.set(Calendar.MONTH, month);//注意Calendar对象默认一月是为零的
        int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);//获得本月份的天数
        //LogManager.show(tag, year+"年"+month+"月的最大天数是："+day, 1);
        return day;
    }

}
